Fix build scripts in dev-dependencies
authorAlex Crichton <alex@alexcrichton.com>
Mon, 8 Dec 2014 20:34:31 +0000 (12:34 -0800)
committerAlex Crichton <alex@alexcrichton.com>
Mon, 8 Dec 2014 20:41:47 +0000 (12:41 -0800)
Don't return dev-dependencies as dependencies for non-test targets as they're
not actually dependencies!

src/cargo/ops/cargo_rustc/context.rs
tests/test_cargo_compile_custom_build.rs

index a743e4f91564d1aead497e6c0fc66d744244485f..def445d898ee50f12afcda3af903f01c0f346d13 100644 (file)
@@ -272,13 +272,22 @@ impl<'a, 'b: 'a> Context<'a, 'b> {
             Some(deps) => deps,
         };
         deps.map(|id| self.get_package(id)).filter(|dep| {
-            // If this target is a build command, then we only want build
-            // dependencies, otherwise we want everything *other than* build
-            // dependencies.
             let pkg_dep = pkg.get_dependencies().iter().find(|d| {
                 d.get_name() == dep.get_name()
             }).unwrap();
-            target.get_profile().is_custom_build() == pkg_dep.is_build()
+
+            // If this target is a build command, then we only want build
+            // dependencies, otherwise we want everything *other than* build
+            // dependencies.
+            let is_correct_dep =
+                target.get_profile().is_custom_build() == pkg_dep.is_build();
+
+            // If this dependency is *not* a transitive dependency, then it
+            // only applies to test targets
+            let is_actual_dep = pkg_dep.is_transitive() ||
+                                target.get_profile().is_test();
+
+            is_correct_dep && is_actual_dep
         }).filter_map(|pkg| {
             pkg.get_targets().iter().find(|&t| self.is_relevant_target(t))
                .map(|t| (pkg, t))
index aef3c77e0088f2c2507a1df3faa216d554d7d7fa..eb3d8eef376c58c89109ffb0afecbf34e09b7251 100644 (file)
@@ -975,3 +975,28 @@ test!(test_a_lib_with_a_build_command {
     assert_that(p.cargo_process("test"),
                 execs().with_status(0));
 })
+
+test!(test_dev_dep_build_script {
+    let p = project("foo")
+        .file("Cargo.toml", r#"
+            [project]
+            name = "foo"
+            version = "0.5.0"
+            authors = []
+
+            [dev-dependencies.a]
+            path = "a"
+        "#)
+        .file("src/lib.rs", "")
+        .file("a/Cargo.toml", r#"
+            [project]
+            name = "a"
+            version = "0.5.0"
+            authors = []
+            build = "build.rs"
+        "#)
+        .file("a/build.rs", "fn main() {}")
+        .file("a/src/lib.rs", "");
+
+    assert_that(p.cargo_process("test"), execs().with_status(0));
+})